package tp.pr1;

import java.util.Scanner; //para poder leer la cadena de entrada en el runGame

public class Game
{

	private Room currentRoom;
	private Parser traductor;
	
	public Game()
	{
		this.currentRoom=null;
		this.traductor=new Parser(); //crea el parser

	}

	protected void changeRoom(Directions direction)
	{
		Room auxiliar = this.currentRoom.getNextRoom(direction);
		
		if (auxiliar==null) 
		{
			System.out.println("The way is closed in direction "+direction.toString());
		}
		
		else 
		{
			this.currentRoom=auxiliar;
			System.out.println(this.currentRoom.getDescription());			
		}
	}

	public Room getCurrentRoom()
	{

		return this.currentRoom;
	}

	public boolean initGame(Room startRoom)
	{
		boolean resultado=true;
		
		if (startRoom==null)
			resultado = false;
		
		else
			this.currentRoom=startRoom;
		
		return resultado;
	}

	protected boolean processCommand(Command command)
	{
		boolean termina=false;
		if (command.isValid()) {
			if (command.getVerb() == VerbCommands.QUIT)
				termina=true;
			else if (command.getVerb() == VerbCommands.HELP)
				System.out.println(this.traductor.getHelp());
		
			else {
				this.changeRoom(command.getDirection());
				termina= this.currentRoom.isExit();
			}
		}
		
		return termina;
	}

	public void runGame()
	{
		boolean endGame=false;
		String cadena;
		Command comando;
		
		//mostrar la descripcion de la habitacion inicial
		System.out.println(this.currentRoom.getDescription());
		
		Scanner entrada = new Scanner(System.in);
		
		
		 //bucle principal de ejecucion del juego
		do{
			System.out.print("> ");
			cadena=entrada.nextLine();
			comando = traductor.nextCommand(cadena);
			
			if (comando != null)
			{				
				endGame = processCommand(comando);
							
				if (!comando.isValid()) System.out.println("What?");				
			}
									
		}while (!endGame);
		
		//mensaje de final de juego
		System.out.println("GAME OVER\nThank you for playing, goodbye.");
		
	}
}
